package com.fanix5.javamvvm.ui.main;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.blankj.utilcode.util.ColorUtils;
import com.fanix5.javamvvm.R;
import com.fanix5.javamvvm.base.MvvmBaseActivity;
import com.fanix5.javamvvm.databinding.ActivityMainBinding;
import com.fanix5.javamvvm.viewmodel.state.MainViewModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem;
import me.majiajie.pagerbottomtabstrip.item.NormalItemView;
import me.majiajie.pagerbottomtabstrip.listener.SimpleTabItemSelectedListener;

public class MainActivity extends MvvmBaseActivity<ActivityMainBinding, MainViewModel> {


    private List<Fragment> fragmentList;

    private HomeFragment mHomeFragment;
    private BbsFragment mBBSFragment;
    private MineFragment mMineFragment;
    private InformationFragment mInformationFragment;

    private int mCurrIndex = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            mCurrIndex = savedInstanceState.getInt("currTabIndex");
        }
        switchFragment(mCurrIndex);

    }

    @Override
    protected int initVariableId() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        initBottomNav();

    }

    @Override
    protected void initViewObservable() {

    }

    private void hideFragments(FragmentTransaction transaction) {
        if (null != mHomeFragment) {
            transaction.hide(mHomeFragment);
        }
        if (null != mInformationFragment) {
            transaction.hide(mInformationFragment);
        }
        if (null != mBBSFragment) {
            transaction.hide(mBBSFragment);
        }
        if (null != mMineFragment) {
            transaction.hide(mMineFragment);
        }
    }

    public void switchFragment(int position) {
        // Fragment事务管理器
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragments(transaction);
        switch (position) {
            //首页
            case 0: //home
                if (mHomeFragment == null) {
                    mHomeFragment = HomeFragment.getInstance();
                    transaction.add(R.id.fragment, Objects.requireNonNull(mHomeFragment), "home");
                } else {
                    transaction.show(mHomeFragment);
                }
                break;

            case 1: //information
                if (mInformationFragment == null) {
                    mInformationFragment = InformationFragment.getInstance();
                    transaction.add(R.id.fragment, Objects.requireNonNull(mInformationFragment), "information");
                } else {
                    transaction.show(mInformationFragment);
                }
                break;

            case 2: //bbs
                if (mBBSFragment == null) {
                    mBBSFragment = BbsFragment.getInstance();
                    transaction.add(R.id.fragment, Objects.requireNonNull(mBBSFragment), "bbs");
                } else {
                    transaction.show(mBBSFragment);
                }

                break;

            default:
                if (mMineFragment == null) {
                    mMineFragment = MineFragment.getInstance();
                    transaction.add(R.id.fragment, Objects.requireNonNull(mMineFragment), "mine");
                } else {
                    transaction.show(mMineFragment);
                }
                break;
        }
        transaction.commitAllowingStateLoss();
    }




    private void initBottomNav() {
        fragmentList = new ArrayList<>();
        fragmentList.add( HomeFragment.getInstance());
        fragmentList.add( InformationFragment.getInstance());
        fragmentList.add( BbsFragment.getInstance());
        fragmentList.add( MineFragment.getInstance());

//        FragmentManager fragmentManager = getSupportFragmentManager();
//        Fragment fragment = fragmentManager.findFragmentByTag("0"); //tag
//
//            fragment = fragmentList.get(0); //没有才新建
//
//        FragmentTransaction transaction = fragmentManager.beginTransaction();
//        transaction
//                .replace(R.id.fragment, fragment) //tag
//                .addToBackStack(null)
//                .commit();




        NavigationController navigationController = mDatabind.bottomNav.custom()
                .addItem(newItem(R.drawable.ic_bottom_nav_home,R.drawable.ic_bottom_nav_home_selected,"首页"))
                .addItem(newItem(R.drawable.ic_bottom_nav_information, R.drawable.ic_bottom_nav_information_selected, "消息"))
                .addItem(newItem(R.drawable.ic_bottom_nav_bbs, R.drawable.ic_bottom_nav_bbs_selected, "论坛"))
                .addItem(newItem(R.drawable.ic_bottom_nav_mine, R.drawable.ic_bottom_nav_mine_selected, "我的"))
                .enableAnimateLayoutChanges()
                .build();

        navigationController.addSimpleTabItemSelectedListener(new SimpleTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {

//                //跳转第二个Fragment
//                FragmentManager fragmentManager2 = getSupportFragmentManager();
//                Fragment toFragment = fragmentManager2.findFragmentByTag(String.valueOf(index));
//
//                    toFragment = fragmentList.get(index);
//
//
//                FragmentTransaction transaction2= fragmentManager2.beginTransaction();
//                transaction2
//                        .replace(R.id.fragment, toFragment)
//                        .addToBackStack(null)
//                        .commit();



//                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//                transaction.replace(R.id.fragment,fragmentList.get(index))
//                        .addToBackStack(null)
//                        .commit();




                switch (index){
                    case 0:
                        switchFragment(0);
                        break;
                    case 1:
                        switchFragment(1);
                        break;
                    case 2:
                        switchFragment(2);
                        break;
                    default:
                        switchFragment(3);
                        break;
                }
            }
        });

    }

    //创建一个Item
    private BaseTabItem newItem(int drawable, int checkedDrawable, String text){
        NormalItemView normalItemView = new NormalItemView(this);
        normalItemView.initialize(drawable,checkedDrawable,text);
        normalItemView.setTextDefaultColor(Color.GRAY);
        normalItemView.setTextCheckedColor(ColorUtils.getColor(R.color.colorPrimary));
        return normalItemView;
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }
}